package com.mrli.cat.weather.tools;

import android.util.Log;

import com.mrli.cat.weather.R;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Utility {

    private static final String[] WEEK = new String[]{
            "周日", "周一", "周二", "周三",
            "周四", "周五", "周六"
    };


    //将天气代码转换为彩色图标资源ID
    public static int convertIDtoWeatherArtIcon(int id) {
        if (id == 800) {
            return R.mipmap.art_clear;
        }
        if (id == 801) {
            return R.mipmap.art_light_clouds;
        }
        if (id >= 802 && id <= 804) {
            return R.mipmap.art_clouds;
        }
        if (id == 741) {
            return R.mipmap.art_fog;
        }
        if (id == 721) {
            return R.mipmap.art_fog;
        }
        if (id == 500) {
            return R.mipmap.art_light_rain;
        }
        if (id >= 501 && id <= 531) {
            return R.mipmap.art_rain;
        }
        if (id >= 600 && id <= 622) {
            return R.mipmap.art_snow;
        }
        if (id >= 200 && id <= 232) {
            return R.mipmap.art_storm;
        }
        return -1;
    }

    //将天气代码转换为灰色图标资源ID
    public static int convertIDtoWeatherICIcon(int id) {
        if (id == 800) {
            return R.mipmap.ic_clear;
        }
        if (id == 721) {
            return R.mipmap.art_fog;
        }
        if (id == 801) {
            return R.mipmap.ic_light_clouds;
        }
        if (id >= 802 && id <= 804) {
            return R.mipmap.ic_cloudy;
        }
        if (id == 741) {
            return R.mipmap.ic_fog;
        }
        if (id == 500) {
            return R.mipmap.ic_light_rain;
        }
        if (id >= 501 && id <= 531) {
            return R.mipmap.ic_rain;
        }
        if (id >= 600 && id <= 622) {
            return R.mipmap.ic_snow;
        }
        if (id >= 200 && id <= 232) {
            return R.mipmap.ic_storm;
        }
        return -1;
    }

    //将天气代码转换为文字
    public static String convertIDtoDes(int id) {
        if (id == 800) {
            return "晴天";
        }
        if (id == 801) {
            return "多云";
        }
        if (id >= 802 && id <= 804) {
            return "阴天";
        }
        if (id == 741) {
            return "雾";
        }
        if (id == 500) {
            return "小雨";
        }
        if (id >= 501 && id <= 531) {
            return "大雨";
        }
        if (id >= 600 && id <= 622) {
            return "雪";
        }
        if (id >= 200 && id <= 232) {
            return "暴雨";
        }
        if (id == 721) {
            return "霾";
        }
        return null;
    }

    //将时间戳转换为正确的时间格式
    public static String convertTimeStampToDate(long timeStamp) {
        //收到的时间戳
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTimeInMillis(timeStamp * 1000);
        StringBuffer buffer = new StringBuffer();
        buffer.append(calendar.get(Calendar.MONTH) + 1);
        buffer.append("月");
        buffer.append(calendar.get(Calendar.DAY_OF_MONTH));
        buffer.append("日");
        buffer.append(WEEK[calendar.get(Calendar.DAY_OF_WEEK) - 1]);
        return buffer.toString();
    }


    //将摄氏度转换为华氏度
    public static String convertCelsiusToFahrenheit(int cel, boolean convert) {
        if (convert) {
            double fah = cel * 1.8 + 32;
            NumberFormat format = new DecimalFormat("#0");
            return format.format(fah) + "°";
        } else {
            return cel + "°";
        }
    }

    //将时间戳转换为正确的时间格式(在DetailActivity)
    public static String convertTimeStampToDetailActivityDate(long timeStamp) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTimeInMillis(timeStamp * 1000);
        StringBuffer buffer = new StringBuffer();
        buffer.append(calendar.get(Calendar.YEAR))
                .append("年")
                .append(calendar.get(Calendar.MONTH) + 1)
                .append("月")
                .append(calendar.get(Calendar.DAY_OF_MONTH))
                .append("日");
        return buffer.toString();
    }

    //将时间戳转换为正确的星期(在DetailActivity)
    public static String convertTimeStampToDetailActivityWeek(long timeStamp) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTimeInMillis(timeStamp * 1000);
        StringBuffer buffer = new StringBuffer();
        buffer.append(WEEK[calendar.get(Calendar.DAY_OF_WEEK) - 1]);
        return buffer.toString();
    }
}
